package BFS;

import javax.sound.midi.SoundbankResource;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * create by Sun
 * 2018/8/21
 * 广度优先遍历
 */
public class cengsishuchu {
    static int[][] map=new int[1000][1000];
    static int[] vis=new int[1000];
    static int n,m,k;
    static LinkedList queue=new LinkedList();
    static void bfs(int k){
        vis[k]=1;
       queue.offer(k);
        System.out.print(k+" ");
        while (!queue.isEmpty()){

            int begin=(int)queue.peek();

            queue.poll();
            for(int i=0;i<n;i++){
                if(map[begin][i]==1&&vis[i]==0){
                    System.out.print(i+" ");
                    vis[i]=1;
                    queue.offer(i);

                }
            }

        }

    }
    public static void main(String[] args) {
        LinkedList list=new LinkedList();
//        list.offer(1);
//        list.offer(2);
//        list.offer(3);
//        System.out.println(  list.size() );
//        System.out.println(list.peek());
        //        list.offer(1);
//        list.offer(2);
//        System.out.println(list.peek());
//        list.poll();
//        System.out.println(list.peek());



        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        k=sc.nextInt();
        for(int i=0;i<m;i++){
            int x=sc.nextInt();
            int y=sc.nextInt();
            map[x][y]=map[y][x]=1;
        }
        vis[k]=1;

        bfs(k);
    }


}
